package cn.ococci.dm.Main.Tools;

import android.text.TextUtils;

import java.util.Date;
import java.util.List;
import java.util.regex.Pattern;

import cn.ococci.dm.Main.Model.DoorUnitModel;

/**
 * Created by cyan on 15/9/21.
 * 字符的工具类
 */
public final class StringTool {

    private final static Pattern phone = Pattern
            .compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9])|(17[0-9]))\\d{8}$");

    /**
     * 判断是不是一个合法的手机号码
     */
    public static boolean isPhone(CharSequence phoneNum) {
        if (isEmpty(phoneNum))
            return false;
        return phone.matcher(phoneNum).matches();
    }


    /**
     * 判断给定字符串是否空白串 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串，返回true
     */
    public static boolean isEmpty(CharSequence input) {
        if (input == null || "".equals(input))
            return true;

        return false;
    }


    public static boolean isPwdToShort(String pwd){
        if (pwd == null || "".equals(pwd))
            return true;

        if(pwd.length() < 6){
            return true;
        }

        return false;
    }


    /**
     * 判断数据是否存在
     * @param data
     * @return
     */
    public static boolean hasData(List<?> data){
         if(data != null  && !data.isEmpty()){
             return true;
         }

        return false;
    }



    public static String replaceDoubleQuotationMarks(String ssid){
        return ssid.replaceAll("\"", "");
    }

    public static String convertToQuotedString(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }

        final int lastPos = string.length() - 1;
        if (lastPos < 0 || (string.charAt(0) == '"' && string.charAt(lastPos) == '"')) {
            return string;
        }

        return "\"" + string + "\"";
    }

    /**
     *  替换所有的符号:针对日期（"-" " ", ":"）
     * @return 20151008154010
     */
    public static String replaceAllSymbol(){
        String time = DateTool.dateToStringWithLong(new Date());
        return time.replace("-","").replace(" ","").replace(":","");
    }


    /**
     * 构建上传的UnitCode列表
     * @param unitCode
     * @return
     */
    public static String builderUnitCode(List<DoorUnitModel> unitCode){

        StringBuilder sb = new StringBuilder();
        for (int i = 0;i<unitCode.size();i++){
            sb.append(""+unitCode.get(i).getUnitCode());
            if (i != unitCode.size() - 1){
                sb.append(",");
            }
        }

        return sb.toString();

    }



    /**
     * 替换冒号
     * @param strValue
     * @return
     */
    public static String replaceSymbol(String strValue){
        return strValue.replace(".", "");
    }


    /**
     *  比较版本
     * @param serverVersion 服务器版本
     * @param doorVersion 门禁版本
     * @return
     */
    public static boolean compareVersion(String serverVersion,String doorVersion){

        if(StringTool.isEmpty(doorVersion)){
            return true;
        }

        if(StringTool.isEmpty(serverVersion)){
            return true;
        }


        LogTool.log(LogTool.CYAN,"比较大小:"+serverVersion + " , " + doorVersion);
        int compareId = serverVersion.compareToIgnoreCase(doorVersion);

        return compareId > 0;

    }


    /**
     *  比较版本
     * @param serverVersion 服务器版本
     * @param doorVersion door版本
     * @return
     */
    public static boolean compareEqualsVersion(String serverVersion,String doorVersion){

        if(StringTool.isEmpty(doorVersion)){
            return false;
        }

        if(StringTool.isEmpty(serverVersion)){
            return false;
        }

        int compareId = serverVersion.compareToIgnoreCase(doorVersion);
        return compareId == 0;

    }






}
